package kong_gui_items;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class VoiceFrequencyWidget extends BaseGraphics  {

	
	private static final long serialVersionUID = -3916931670762611120L;
	
	//Double between 0 and 1
	private double frequencyLevel = 0.5; 
	String [] widgetState;
	
	int RELEASED_LEVEL = 0;
	int LOCK_LEVEL = 1;
	
	public VoiceFrequencyWidget(int x, int y) {
		super(x, y, 50, 100);
		
		
		widgetState = new String[]{".\\Resources\\Picts\\WidgetFrame_Speak.png",
				   ".\\Resources\\Picts\\WidgetFrame_Locked.png"};
		
		releaseVoiceLevel();
		
	}
	
	@Override
	public void paint(Graphics g) {
			
			Graphics2D g2d = (Graphics2D)g;
			
			int rectangleHeight = (int) ((this.height) * frequencyLevel) ;
			
			Rectangle level = new Rectangle(this.x,
					(this.y)+(this.height)-rectangleHeight ,
					this.width,
					rectangleHeight);
			
			//Save the initial color
			Color initialColor = g2d.getColor();
			g2d.drawImage(image,this.x-22,this.y-10, image.getWidth(), image.getHeight(), null);
			
			//Draw the level rectangle
			g2d.setColor(Color.yellow);
			g2d.fill(level);
			
			//Set back the original color
			g2d.setColor(initialColor);
	}


	public void releaseVoiceLevel (){
		try {image = ImageIO.read(new File(widgetState[RELEASED_LEVEL]));	} 
		catch (IOException e) {e.printStackTrace();}
	}
	
	public void lockVoiceLevel (){
		try {image = ImageIO.read(new File(widgetState[LOCK_LEVEL]));	} 
		catch (IOException e) {e.printStackTrace();}
	}
	
	
	public void setFrequencyLevel(double frequencyLevel) {
		this.frequencyLevel = frequencyLevel;
	}

	public double getFrequencyLevel() {
		return frequencyLevel;
	}
}
